var app = getApp();
var sliderWidth = 187.5; // 需要设置slider的宽度，用于计算中间位置
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["推荐关注", "成交会员"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    list1: [],
    list2: [],
    id: 0,
    page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });

    Getglist(that, 1, app.globalData.userid, 1) 
  },

  tabClick: function (e) {
    var that = this
    that.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    console.log(e.currentTarget.offsetLeft)
    if (e.currentTarget.id == 0) {
      //推荐关注
      that.setData({
        page: 1
      })
      Getglist(that, 1, app.globalData.userid, 1)
    }
    if (e.currentTarget.id == 1) {
      //成交会员
      that.setData({
        page: 1
      })
      Getglist(that, 1, app.globalData.userid, 2)
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if (that.data.activeIndex == 0) {
      Getglist(that, that.data.page, app.globalData.userid, 1)
    }
    if (that.data.activeIndex == 1) {
      Getglist(that, that.data.page, app.globalData.userid,2)
    }
  },

})

var url = "https://17fx.com/weapp/Adminapi/lower_list.html";
var page_size = 10
//type 1 type 2
var Getglist = function (that, page, user_id, type) {
  wx.request({
    url: url,
    data: {
      user_id: user_id,
      type: type,
      page: page,
      page_size: page_size
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      console.log(res.data);
      if (page == 1) {
        that.setData({
          list1: []
        })
      }
      if (res.data != null) {
        var list1 = that.data.list1;
        for (var i = 0; i < res.data.length; i++) {
          list1.push(res.data[i]);
        }
        that.setData({
          list1: list1,
          page: that.data.page + 1
        })
      }

    }
  })
}